package control_monitor;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Files {

	private FileInputStream instream;
	private BufferedReader buff;
	
	public Files()
	{
		try {
			instream = new FileInputStream("Incidencia.txt");
			buff = new BufferedReader(new InputStreamReader(instream));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Read(int Plazas, int Transiciones, int[][] vectmatriz, int[] vectmarcado)
	{
		int Plaza=0, Transicion=0, cont=-1, recorrido, cantaux;
		String caracter;
		try {
			while((caracter = buff.readLine()) != null)
			{
				recorrido=0;
				cantaux=0;		
				
				while(recorrido < caracter.length())
				{
					cantaux=0;
					
					if(caracter.charAt(recorrido) == 45 || (caracter.charAt(recorrido) > 47 && caracter.charAt(recorrido) < 58))
					{
						if(caracter.charAt(recorrido) == '-')
						{
							recorrido++;
							cantaux++;
							if(recorrido+1 < caracter.length())
							{
								while((caracter.charAt(recorrido+1) > 47 && caracter.charAt(recorrido+1) < 58))
								{
									cantaux++;
									recorrido++;
									
									if((recorrido+1) == caracter.length())
									{
										break;
									}
								}
							}

							if(cont==-1)
							{																														
								vectmatriz[Plaza][Transicion]=(-(Calcularint(caracter, cantaux, recorrido)));
							}
							else
							{
								vectmarcado[cont]=(-(Calcularint(caracter, cantaux, recorrido)));;
							}
							
							if(!((Transicion+1)==Transiciones))
							{
							Transicion++;
							}
							else
							{
								if(!((Plaza+1)==Plazas))
								{
									Plaza++;
									Transicion=0;
								}
								else
								{
									cont++;
								}
							}					
						}
						
						else
						{	
							cantaux++;
							if(recorrido+1 < caracter.length())
							{
								while((caracter.charAt(recorrido+1) > 47 && caracter.charAt(recorrido+1) < 58))
								{
									cantaux++;
									recorrido++;
									
									if((recorrido+1) == caracter.length())
									{
										break;
									}
								}
							}
							
							if(cont==-1)
							{
								vectmatriz[Plaza][Transicion]=((Calcularint(caracter, cantaux, recorrido)));
							}
							else
							{
								vectmarcado[cont]=((Calcularint(caracter, cantaux, recorrido)));
							}
							
							if(!((Transicion+1)==Transiciones))
							{
							Transicion++;
							}
							else
							{
								if(!((Plaza+1)==Plazas))
								{
									Plaza++;
									Transicion=0;
								}
								else
								{
									cont++;
								}
							}
						}
						recorrido++;
					}
					else
					{
						recorrido++;
					}		
			}
			} 
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void Cerrar()
	{
		try {
			instream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private int Calcularint(String cadena, int cantaux, int recorrido)
	{
		int resultado=0;
		
		for(int i=0; i<cantaux; i++)
		{
			resultado = resultado + ((cadena.charAt(recorrido-i)-48)*((int)(Math.pow(10, i))));			
		}
		
		return resultado;		
	}


	

}
